package com.game.demo409.demo409;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;

import com.actionbarsherlock.app.SherlockActivity;
import com.game.demo409.demo409.R;
import com.yalantis.pulltorefresh.library.PullToRefreshView;

import java.util.LinkedList;

import views.LoadMoreListView;

public class MessageActivity extends SherlockActivity {
    private static final int LOAD_DELAY = 1500 ;
    private static final int REFRESH_DELAY = 1500;
    private PullToRefreshView mPullToRefreshView;
    private LoadMoreListView message_list;
    private LinkedList<String> data;
    private ArrayAdapter<String> mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        mPullToRefreshView = (PullToRefreshView)findViewById(R.id.pull_to_refresh_message);
        message_list = (LoadMoreListView)findViewById(R.id.message_test);
        data = new LinkedList<String>();
        data.add("消息1");
        data.add("消息2");
        data.add("消息3");
        data.add("消息4");
        data.add("消息5");
        data.add("消息6");
        data.add("消息7");
        data.add("消息8");
        data.add("消息8");
        data.add("消息8");
        data.add("消息8");
        data.add("消息8");
        mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,data);
        message_list.setOnGetMoreListener(new LoadMoreListView.OnGetMoreListener() {
            @Override
            public void onGetMore() {
                message_list.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        data.add("消息+");
                        mAdapter.notifyDataSetChanged();
                        message_list.getMoreComplete();
                    }
                }, LOAD_DELAY);
            }
        });
        message_list.setAdapter(mAdapter);
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        data.addFirst("新消息");
                        mAdapter.notifyDataSetChanged();
                        mPullToRefreshView.setRefreshing(false);
                    }
                }, REFRESH_DELAY);
            }
        });
    }


//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.menu_message, menu);
//        return true;
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        // Handle action bar item clicks here. The action bar will
//        // automatically handle clicks on the Home/Up button, so long
//        // as you specify a parent activity in AndroidManifest.xml.
//        int id = item.getItemId();
//
//        //noinspection SimplifiableIfStatement
//        if (id == R.id.action_settings) {
//            return true;
//        }
//
//        return super.onOptionsItemSelected(item);
//    }
}
